package com.jelly.web.modules.basics.websiteDetails.service.impl;

import com.jelly.common.enums.WebsiteDetailsType;
import com.jelly.common.util.ListUtil;
import com.jelly.web.modules.basics.websiteDetails.dao.WebsiteDetailsMapper;
import com.jelly.web.modules.basics.websiteDetails.domain.*;
import com.jelly.web.modules.basics.websiteDetails.service.IWebsiteDetailsService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @创建人 ql
 * @创建时间 2018-10-11 14:25
 * @项目名称 jelly
 * @功能描述: 网站详情接口实现类
 **/
@Service
public class WebsiteDetailsServiceImpl implements IWebsiteDetailsService {

    @Autowired
    private WebsiteDetailsMapper websiteDetailsMapper;

    @Override
    public List<WebsiteDetails> getWebsiteDetailsAll() {

        List<WebsiteDetails> list = websiteDetailsMapper.selectByExample(new WebsiteDetailsExample());

        if(ListUtil.isNotList(list)){

            for (WebsiteDetails websiteDetails:list){

                JSONObject jsonObject=JSONObject.fromObject(websiteDetails.getDetails());

                if(WebsiteDetailsType.WEBESSEBTIAL.getValue().equals(websiteDetails.getId())){
                    websiteDetails.setWebEssential((WebEssential)JSONObject.toBean(jsonObject, WebEssential.class));
                }else if(WebsiteDetailsType.WEBMAIL.getValue().equals(websiteDetails.getId())){
                    websiteDetails.setWebMail((WebMail)JSONObject.toBean(jsonObject, WebMail.class));
                }else if(WebsiteDetailsType.WEBUPLOAD.getValue().equals(websiteDetails.getId())){
                    websiteDetails.setWebUpload((WebUpload) JSONObject.toBean(jsonObject, WebUpload.class));
                }

            }

        }

        return list;

    }

    @Override
    public boolean updateWebsiteDetails(WebsiteDetails websiteDetails) {
        JSONObject json = null;
        if(WebsiteDetailsType.WEBESSEBTIAL.getValue().equals(websiteDetails.getId())){
            json = JSONObject.fromObject(websiteDetails.getWebEssential());
        }else if(WebsiteDetailsType.WEBMAIL.getValue().equals(websiteDetails.getId())) {
            json = JSONObject.fromObject(websiteDetails.getWebMail());
        }else if(WebsiteDetailsType.WEBUPLOAD.getValue().equals(websiteDetails.getId())){
            json = JSONObject.fromObject(websiteDetails.getWebUpload());
        }else{
            return false;
        }
        websiteDetails.setDetails(json.toString());

        return websiteDetailsMapper.updateByPrimaryKeySelective(websiteDetails) > 0;

    }

    @Override
    public WebsiteDetails getWebsiteDetails(String id) {
        WebsiteDetails websiteDetails = websiteDetailsMapper.selectByPrimaryKey(id);
        JSONObject jsonObject=JSONObject.fromObject(websiteDetails.getDetails());
        if(WebsiteDetailsType.WEBESSEBTIAL.getValue().equals(websiteDetails.getId())){
            websiteDetails.setWebEssential((WebEssential)JSONObject.toBean(jsonObject, WebEssential.class));
        }else if(WebsiteDetailsType.WEBMAIL.getValue().equals(websiteDetails.getId())){
            websiteDetails.setWebMail((WebMail)JSONObject.toBean(jsonObject, WebMail.class));
        }else if(WebsiteDetailsType.WEBUPLOAD.getValue().equals(websiteDetails.getId())){
            websiteDetails.setWebUpload((WebUpload) JSONObject.toBean(jsonObject, WebUpload.class));
        }
        return websiteDetails;
    }

}
